package com.llb.ui;



import com.llb.entity.ZjjhBean;
import com.llb.utils.ExcelReader;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

/**
 * @ClassName parseExcelUI
 * @Description TODO
 * @Author liang
 * @Date 2019/3/22 23:14
 * @Version 1.0
 **/
public class ParseExcelUI extends JFrame {
    //定义组件
    JPanel jPanel;
    JScrollPane jScrollPane;
    JTextField textField;
    JTextArea textArea;
    JButton uploadBt;
    JButton parseBt;
    JFileChooser chooser;//文件上传
    String choosertitle;
    public static final String extensively = "友情提示\n"
            + "1. 请勿删除文件夹下的,jre,目录\n"
            + "2. Excel文件请严格按照49项顺序,可以参考附件\n"
            + "3. 上传文件后,点击校验,如果数据有误,请根据提示修改,如果数据无误,请确认校验数据量是否正确\n"
            + "4. 社保经办机构,险种类型代码,统筹区行政区划,社保业务系统编码等如果有误,可以参考附件代码表\n";
    /**
     * 构造面板
     */
    public ParseExcelUI(){
        jPanel = new JPanel();
        textArea = new JTextArea();
        jScrollPane = new JScrollPane(textArea);
        textField = new JTextField(30);
        uploadBt = new JButton("文件上传");
        parseBt = new JButton("校验");
        //说明
        textArea.setText(extensively);
        jPanel.add(textField);
        jPanel.add(uploadBt);
        jPanel.add(parseBt);
        //布局
        this.add(jScrollPane);
        this.add(jPanel, BorderLayout.SOUTH);
        this.setSize(800, 600);
        this.setTitle("征缴计划校验工具V1.0");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        //监听按钮
        action();

    }

    public void action(){
        //上传动作
        uploadBt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                chooser = new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                //默认打开当前文件所在的目录
                chooser.setCurrentDirectory(new File("."));
                chooser.setDialogTitle(choosertitle);
                chooser.addChoosableFileFilter(new xlsxFilter()); // 文件过滤器
                chooser.addChoosableFileFilter(new xlsFilter()); // 文件过滤器


                if(chooser.showOpenDialog(jPanel) == JFileChooser.APPROVE_OPTION){
                    //File currentDirectory = chooser.getCurrentDirectory();
                    File selectedFile = chooser.getSelectedFile();
                    textField.setText(selectedFile.getAbsolutePath());
                    System.out.println("选择的文件路径为:"+selectedFile);
                }else {
                    System.out.println("没选择文件");
                }


            }
        });


        //解析动作
        parseBt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("");
                if(textField.getText().length() == 0){
                    textArea.setText("请选择文件");
                }else {
                    ExcelReader excelReader = new ExcelReader();
                    try {
                        List<Object> objects = excelReader.readExcelContent(textField.getText(), ZjjhBean.class);
                        int count = 0 ;
                        for(Object o : objects){
                            ZjjhBean zj = (ZjjhBean) o;
                            String result = ((ZjjhBean) o).parse();
                            if(result.length() > 3){
                                textArea.append("征集通知流水号为"+zj.getZjtzlsh()+"的单位数据不对,错误原因为:"+result+"\n");
                                count++;
                            }else if("有空行".equals(result)){
                                textArea.append("请检查文末是否有空行,请删除空行\n");
                                count++;
                            }else {

                            }
                        }
                        if(count == 0){
                            textArea.append("恭喜!本次数据全部正确,共计"+objects.size()+"条,请确认数据量是否正确\n");
                        }else {
                            textArea.append("本次数据错误数据共计"+count+"条,请根据提示进行修改!!\n");

                        }

                    } catch (Exception e1) {
                        e1.printStackTrace();
                        textArea.setText(e1.toString());
                    }
                }
            }
        });
    }



    //文件过滤

    class xlsFilter extends FileFilter {

        @Override
        public boolean accept(File f) {
            if (f.isDirectory())return true;
            return f.getName().toLowerCase().endsWith(".xls");
        }

        @Override
        public String getDescription() {
            return ".xls";
        }
    }

    class xlsxFilter extends FileFilter{

        @Override
        public boolean accept(File f) {
            if (f.isDirectory())return true;
            return f.getName().toLowerCase().endsWith(".xlsx");
        }

        @Override
        public String getDescription() {
            return ".xlsx";
        }
    }
}
